﻿#include "FITKAcousticsVTKGraphObjectSEAJunctions.h"

// VTK
#include <vtkProperty.h>
#include <vtkPoints.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkCompositeDataGeometryFilter.h>
#include <vtkMapper.h>
#include <vtkCellData.h>
#include <vtkExtractBlock.h>

// App
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"

// Data
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"

// Graph and filter
#include "FITKAcousticsVTKCommons.h"
#include "FITKAcousticsVTKSharedDataManager.h"
#include "FITKAcousticsVTKGraphObjectSEASelectMergedData.h"
#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActor.h"

namespace Exchange
{
    FITKAcousticsVTKGraphObjectSEAJunctions::FITKAcousticsVTKGraphObjectSEAJunctions(Acoustics::FITKAcousticsDiscModel* seaModel)
        : FITKAcousticsVTKGraphObjectMergedData(seaModel)
    {
        if (!seaModel)
        {
            return;
        }

        // Set the layer need to be rendered.
        setRenderLayer(0, 2);

        // Get the SEA junction manager.
        m_SEAJunctionMgr = seaModel->getJunctionManager();

        // Change the highlight color.
        if (m_highlightSelector)
        {
            m_highlightSelector->setColor(Qt::yellow);
        }
    }

    void FITKAcousticsVTKGraphObjectSEAJunctions::update(bool forceUpdate)
    {
        this->Superclass::update(forceUpdate);

        // Update the glyph size.
        updateGlyphSizeByRatio();
    }

    void FITKAcousticsVTKGraphObjectSEAJunctions::updateVisibility()
    {
        // Update the blocks' visibility.
        if (m_SEAJunctionMgr && m_extractBlock && m_multiBlockDataSet){
            // Clear the extract blocks.
            m_extractBlock->RemoveAllIndices();
            m_multiBlockDataSet->ReleaseData();

            int index = 0;
            for (Acoustics::FITKAbstractJunction* junc : m_SEAJunctionMgr->getJointsByType(m_SEAJunctionTypeEnum)){
                if (junc == nullptr){
                    continue;
                }

                // Check the data visibility.
                if (!junc->isEnable())
                {
                    continue;
                }

                int dataIndex = -1;
                vtkDataSet* ds = FITKAcousticsVTKSharedDataManager::GetInstance()->getSEAJunctionDataSet(junc->getDataObjectID(), dataIndex);
                Q_UNUSED(ds);

                if (dataIndex < 0)
                {
                    continue;
                }
                m_multiBlockDataSet->SetBlock(index,ds);
                m_extractBlock->AddIndex(index);
                index++;
            }
        }

        // Update the whole object's visibility.
        this->Superclass::updateVisibility();
    }

    void FITKAcousticsVTKGraphObjectSEAJunctions::updateById(int dataId, bool forceUpdate)
    {
        int dataIndex = -1;
        vtkDataSet* dataSet = FITKAcousticsVTKSharedDataManager::GetInstance()->getSEASubSystemDataSet(dataId, dataIndex);
        Q_UNUSED(dataIndex);

        // The data does not exist or the 'forceUpdate' is true.
        if (!dataSet || forceUpdate)
        {
            // Re-generate the dataset.
            FITKAcousticsVTKSharedDataManager::GetInstance()->updateSEAJunctionDataSet(dataId);
        }
        else
        {
            // Update the color only.
            FITKAcousticsVTKSharedDataManager::GetInstance()->updateSEAJunctionDataSetColor(dataId);
        }
    }

    bool FITKAcousticsVTKGraphObjectSEAJunctions::getDataVisibility()
    {
        return getGlobalSEAJunctionVisibility();
    }

    bool FITKAcousticsVTKGraphObjectSEAJunctions::getGlobalSEAJunctionVisibility()
    {
        // Get the runtime settings for visibility. 
        bool showJunc = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_JUNCTIONS, true);
        return showJunc;
    }
}   // namespace Exchange